package com.olah.user.service;

import com.olah.common.entity.po.user.AddFriendConfig;
import com.olah.common.entity.po.user.Friend;
import com.olah.common.entity.po.user.QuestionAndAnswer;
import com.olah.user.dao.FriendDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendService {

    @Resource
    private FriendDao dao;

    public AddFriendConfig queryAddMethod(int targetId) {
        Integer mode = dao.queryAddMethod(targetId);
        List<QuestionAndAnswer> list = queryQuestions(targetId);
        return new AddFriendConfig(targetId, mode, list);
    }

    public boolean updateAddMethod(AddFriendConfig info) {
        int count = dao.updateAddMethod(info);
        if (info.getMode() == 3) {
            updateQA(info);
        }
        return count == 1;
    }

    public String noteName(int myId, int anotherId) {
        Map<String, Integer> map = new HashMap<>();
        map.put("myId", myId);
        map.put("anotherId", anotherId);
        return dao.noteName(map);
    }

    public List<QuestionAndAnswer> queryQuestions(int targetId) {
        return dao.queryQuestions(targetId);
    }

    public int updateQA(AddFriendConfig info) {
        System.out.println("update qa");
        return dao.updateQA(info);
    }

    public boolean isFriend(int myId, int anotherId) {
        Map<String, Integer> map = new HashMap<>();
        map.put("myId", myId);
        map.put("anotherId", anotherId);
        return 1 == dao.isFriend(map);
    }

    public List<Friend> queryMyFriends(Integer id) {
        return dao.queryMyFriends(id);
    }
}
